package com.air.controller;

import com.air.config.Result;
import com.air.entity.City;
import com.air.entity.Order;
import com.air.entity.Passenger;
import com.air.service.CityService;
import com.air.service.OrderService;
import com.air.service.PassengerService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class PassengerController {

    @Autowired
    PassengerService passengerService;

    @Autowired
    OrderService orderService;

    @RequestMapping(value = "/pg",method = RequestMethod.POST)
    @ResponseBody
    public Result insertPg(@RequestParam(value = "pgs") String pgs,@RequestParam(value = "flightId")  int flightId,@RequestParam("cang") int cang, HttpSession session){
        Gson gson = new Gson();
        List<Passenger> persons =gson.fromJson(pgs, new TypeToken<List<Passenger>>() {}.getType());
        //System.out.println("乘客列表"+persons);
        Object id1 = session.getAttribute("id");
        if (id1 == null){

            return Result.fail("未登录");
        }
        long id = (long) id1;

        //TODO 判断是否已经购买该票

        //Order orderCheck = orderService.getOrderByYiDaDui(flightId,id,cang);
        System.out.println("用户id为"+id);
        Order order = passengerService.insertPg(persons,flightId,id,cang);
        if (order == null){
            return Result.fail("订单异常");

        }
        return Result.success("登陆成功", order);
    }

}
